 # 6.优化模型（网格搜索调参）

#导入包
from sklearn.model_selection import GridSearchCV

# 定义参数网格
param_grid = {
    'n_estimators': [50, 100, 200],
    'max_depth': [None, 5, 10]
}

# 网格搜索
grid_search = GridSearchCV(RandomForestClassifier(random_state=42), param_grid, cv=5)
grid_search.fit(X_train, y_train)

# 最佳参数和模型
print("最佳参数:", grid_search.best_params_)
best_model = grid_search.best_estimator_